package com.yunxi.service.dubboService.impl.otd;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yunxi.core.web.DictConstant;
import com.yunxi.hubToBiz.pojo.MonthPlanBackVO;
import com.yunxi.hubToBiz.pojo.WeekPlanBackVO;
import com.yunxi.hubToBiz.service.ProducePlanBakDubboService;
import com.yunxi.model.otd.OtdOrderSo;
import com.yunxi.model.sto.VehicleOffline;
import com.yunxi.service.localService.otd.OtdIfMonthPlanService;
import com.yunxi.service.localService.otd.OtdIfProducePlanService;
import com.yunxi.service.localService.otd.OtdOrderCirService;
import com.yunxi.service.localService.otd.OtdOrderSoService;
import com.yunxi.service.localService.sto.VehicleOfflineService;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

/**
 * 周、月接口反馈
 *
 * @author XUQI
 */
public class ProducePlanBakDubboServiceImpl implements ProducePlanBakDubboService {

    private static final Logger logger = LoggerFactory.getLogger(OtdWeekPlanAuditDubboServiceImpl.class);

    @Autowired
    @Qualifier("otdIfProducePlanServiceImpl")
    OtdIfProducePlanService ifPlanService;


    @Autowired
    @Qualifier(value = "otdIfMonthPlanServiceImpl")
    OtdIfMonthPlanService ifMonthPlanService;

    @Autowired
    @Qualifier(value = "vehicleOfflineServiceImpl")
    VehicleOfflineService carService;

    @Autowired
    @Qualifier("otdOrderSoServiceImpl")
    OtdOrderSoService soService;

    @Autowired
    @Qualifier("otdOrderCirServiceImpl")
    OtdOrderCirService cirService;

    /**
     * 周度接口反馈
     *
     * @param backVOList 反馈List
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void weekBackProc(List<WeekPlanBackVO> backVOList) {
        if (CollectionUtils.isEmpty(backVOList)) {
            logger.debug("待处理数据为空");
            return;
        }
        for (WeekPlanBackVO backVO : backVOList) {
            LambdaQueryWrapper<OtdOrderSo> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(OtdOrderSo::getPlanNo, backVO.getPlanno());
            OtdOrderSo orderSo = soService.getOne(queryWrapper);
            if (orderSo == null) {
                logger.debug("未找到生产计划，PlanNo：" + backVO.getPlanno());
                continue;
            }

            // 更新发送状态
            orderSo.setSapState(backVO.getZstat());
            orderSo.setSapNo(backVO.getSapno());
            orderSo.setSapInfo(backVO.getZmess());
            orderSo.setUpdateTime(new Date());
            orderSo.setUpdateBy("SAP");
            soService.updateById(orderSo);
//            // 反馈是否成功
//            if ("S".equals(backVO.getZstat())) {
//                //成功
//                //1.更新接口状态
//                orderSo.setSapState("S");//成功
//                orderSo.setSapNo(backVO.getSapno());
//                orderSo.setSapInfo(backVO.getZmess());
//                orderSo.setUpdateTime(new Date());
//                orderSo.setUpdateBy("SAP");
//                soService.updateById(orderSo);

                //2.判断接口类型 I新增  U更新 D删除
//                if ("I".equals(orderSo.getInterfaceType())) {
//                    //2.1 I新增  新增车辆表  车辆扭转状态为生产计划 DictConstant.VEHICLE_TURN_STATE_01DSC;
//                    VehicleOffline car = new VehicleOffline();
//                    car.setZordNo(orderSo.getPlanNo());
//                    car.setZwerks(orderSo.getWorks());
//                    car.setCarType(orderSo.getOrderType());// 车辆类型
//                    car.setProductCode(orderSo.getProductCode());
//                    car.setCustomPackCode(orderSo.getCustomPackCode());
//                    car.setCarState(DictConstant.VEHICLE_TURN_STATE_01DSC);//生产计划
//                    car.setCreateTime(new Date());
//                    car.setCreateBy("SAP");
//                    car.setUpdateTime(new Date());
//                    car.setUpdateBy("SAP");
//                    carService.save(car);
//                } else if ("U".equals(orderSo.getInterfaceType())) {
//                    //3.2 U更新  更新车辆表 产品信息
//                    VehicleOffline car = carService.selectCarByPlanNo(orderSo.getPlanNo());
//                    car.setProductCode(orderSo.getProductCode());
//                    car.setCustomPackCode(orderSo.getCustomPackCode());
//                    carService.updateById(car);
//                } else if ("D".equals(orderSo.getInterfaceType())) {
//                    //3.3 D删除  删除车辆表
//                    carService.removeVehicleCarByPlanNo(orderSo.getPlanNo());
//                }
//            } else {
//                //失败
//                orderSo.setSapState("E");//失败
//                orderSo.setSapNo(backVO.getSapno());
//                orderSo.setSapInfo(backVO.getZmess());
//                orderSo.setUpdateTime(new Date());
//                orderSo.setUpdateBy("SAP");
//                soService.updateById(orderSo);
//            }
        }
    }

    /**
     * 月度接口反馈
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void monthBackProc(List<MonthPlanBackVO> backVOList) {
        for (MonthPlanBackVO back : backVOList) {
            cirService.updateIfPlanBySapBack(back.getPlanno(), back.getSapno(), back.getZstat(), back.getZmess());
        }
    }
}
